﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Xml;

namespace Parallel3M.parallel3m.framework
{
    public class DBParallelSinker:ParallelSinker
    {
        //db connection parameter
        public string dbConnectionUri="server=localhost;User Id=root;database=paralleldb;password=djmachust";
        public MySqlConnection mysqlConnection;
        public string insertSQL = "insert into payload values( @payloadid, @routeTag,@domain,@type,@handlerid,@access,@language,@timestamp,@request,@response,@status,@pcss,@pcsd)";

        public DBParallelSinker()
        {
                     
        }

        public DBParallelSinker(string uri)
        {
            dbConnectionUri=uri;
            mysqlConnection = new MySqlConnection(uri);
            mysqlConnection.Open();
           
        }
        
        //override the process
        public override void process()
        {
            foreach (ParallelConnection inputConnection in inputConnections)
            {
                ParallelPayload payload = inputConnection.Flowout();
                if (payload != null)
                {
                    payloadcount++;
                    Save2DB(payload);
                }
            }
        }

        public override void destroy()
        {
            mysqlConnection.Close();
        }

        public override ParallelPayload getPayload(string paylod_id)
        {
            return null;
        }

        public void Save2DB(ParallelPayload payload)
        {
            
            MySqlCommand mysqlCommand = new MySqlCommand();
            mysqlCommand.Connection = mysqlConnection;
            mysqlCommand.CommandType = System.Data.CommandType.Text;
            mysqlCommand.CommandText = insertSQL;
            mysqlCommand.Parameters.AddWithValue("@payloadid", payload.id);
            mysqlCommand.Parameters.AddWithValue("@routeTag", payload.routeTag);
            mysqlCommand.Parameters.AddWithValue("@domain", payload.domain);
            mysqlCommand.Parameters.AddWithValue("@type", payload.type);
            mysqlCommand.Parameters.AddWithValue("@handlerid", payload.handlerid);
            mysqlCommand.Parameters.AddWithValue("@access", payload.access);
            mysqlCommand.Parameters.AddWithValue("@language", payload.language);
            mysqlCommand.Parameters.AddWithValue("@timestamp", payload.timestamp);
            mysqlCommand.Parameters.AddWithValue("@request", payload.request);
            mysqlCommand.Parameters.AddWithValue("@response", payload.response);
            mysqlCommand.Parameters.AddWithValue("@status", payload.status);
            mysqlCommand.Parameters.AddWithValue("@pcss", payload.pcs_source);
            mysqlCommand.Parameters.AddWithValue("@pcsd", payload.pcs_des);
            mysqlCommand.ExecuteNonQuery();            
        }


        //
        public override void PreParse()
        {
            base.PreParse();
            foreach (XmlNode childnode in node.ChildNodes)
            {
                if (childnode.Name == "url")
                {
                    dbConnectionUri = childnode.InnerText;
                }
            }
            mysqlConnection = new MySqlConnection(dbConnectionUri);
            mysqlConnection.Open();
        }

    }
}
